package net.vinote.smart.platform.service.dw.html;

import java.util.Map;

import net.vinote.smart.platform.service.SRE;
import net.vinote.smart.platform.service.bean.CodeLibrary;


public class InputCheckBoxGroupTag extends HtmlTag {
	private String htmlTemplate = "<ul class='checkBoxGroup'>#content</ul>";

	public InputCheckBoxGroupTag(HtmlTag htmlTag) {
		super(htmlTag);
	}

	@Override
	protected String getHtmlTemplate() {
		return htmlTemplate;
	}

	@Override
	protected void initPlaceHolder() {
		if ("Code".equals(getHtmlSource())) {
			addContent(getCheckboxsByCode());
		}
		placeholder.put("#content", getContent());
	}

	private String getCheckboxsByCode() {
		StringBuffer sb = new StringBuffer();
		Map<String, CodeLibrary> codeItemMap = SRE.getCodeItems(getHtmlVal());
		if (codeItemMap == null || codeItemMap.size() == 0) {
			return "<li><span style='color:red'>未定义</span></li>";
		}
		int index = 0;
		HtmlTag tempTag=this.clone();
		for (String itemNo : codeItemMap.keySet()) {
			InputCheckBoxTag tag = new InputCheckBoxTag(tempTag);
			tag.removeAttribute("id");
			tag.setId(tag.getName() + index++);
			tag.setChecked(getValue().indexOf(itemNo)>=0);
			tag.setValue(itemNo);
			tag.setShowValue(codeItemMap.get(itemNo).getItemName());
			sb.append("<li>");
			sb.append(tag.getHtml());
			sb.append("</li>");
		}
		return sb.toString();
	}

}
